import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    // 登录
    {
      path: '/',
      name: 'login',
      component: () => import('../views/LoginReg/LoGin.vue')
    },
    // 注册
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/LoginReg/RegiSter.vue')
    },
    // 首页
    {
      path: '/shouye',
      name: 'shouye',
      component: () => import('../views/ShouYe/ShouYe.vue')
    },
    //忘记密码
    {
      path: '/edit',
      name: 'edit',
      component: () => import('../views/LoginReg/EdIt.vue')
    },
    //修改个人信息
    {
      path: '/xiuxin',
      name: 'xiuxin',
      component: () => import('../views/LoginReg/XiuXin.vue'),
      meta: { isAuth: true }
    },
    //个人中心
    {
      path: '/geren',
      name: 'geren',
      component: () => import('../views/LoginReg/GeRen.vue'),
      meta: { isAuth: true }
    },
    // 领养
    {
      path: '/adopt',
      name: 'adopt',
      component: () => import('../views/lingyang/AdOpt.vue'),
      meta: { isAuth: true },
      children: [
        {
          path: '',
          name: 'lylist',
          component: () => import('../views/lingyang/LingYang.vue'),
          meta: { isAuth: true }
        },
        {
          path: 'lyjilu',
          name: 'lyjilu',
          component: () => import('../views/lingyang/JiLu.vue'),
          meta: { isAuth: true }
        },
        {
          path: 'shenhe',
          name: 'shenhe',
          component: () => import('../views/lingyang/ShenHe.vue'),
          meta: { isAuth: true }
        }
      ]
    },
    //填写领养信息
    {
      path: '/lyxinxi',
      name: 'lyxinxi',
      component: () => import('../views/lingyang/LyXinxi.vue'),
      meta: { isAuth: true }
    },
    //救助
    {
      path: '/help',
      name: 'help',
      component: () => import('../views/jiuzhu/JiuZhu.vue'),
      meta: { isAuth: true },
      children: [
        {
          path: '',
          name: 'fajiu',
          component: () => import('../views/jiuzhu/FaqiJiuzhu.vue'),
          meta: { isAuth: true }
        },
        {
          path: 'dengdai',
          name: 'dengdai',
          component: () => import('../views/jiuzhu/DengDai.vue'),
          meta: { isAuth: true }
        },
        {
          path: 'jiuzhong',
          name: 'jiuzhong',
          component: () => import('../views/jiuzhu/JiuZhong.vue'),
          meta: { isAuth: true }
        },
        {
          path: 'wanjiu',
          name: 'wanjiu',
          component: () => import('../views/jiuzhu/WanJiu.vue'),
          meta: { isAuth: true }
        }
      ]
    },
    //论坛
    {
      path: '/forum',
      name: 'forum',
      component: () => import('../views/luntan/FoRum.vue'),
      meta: { isAuth: true },
      children: [
        {
          path: '',
          name: 'fabu',
          component: () => import('../views/luntan/LunTan.vue'),
          meta: { isAuth: true }
        },
        {
          path: 'myforum',
          name: 'myforum',
          component: () => import('../views/luntan/MyForum.vue'),
          meta: { isAuth: true }
        },
        {
          path: 'details',
          name: 'details',
          component: () => import('../views/luntan/LunDetails.vue'),
          meta: { isAuth: true }
        }
      ]
    },
    // 养宠小知识
    {
      path: '/knowledge',
      name: 'knowledge',
      component: () => import('../views/xiaozhishi/KnowLedge.vue'),
      meta: { isAuth: true }
    },
    //404页面
    {
      path: '/NaN404',
      name: 'NaN404',
      component: () => import('../views/404/NanView.vue')
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查即将进入的路由是否匹配到了路由记录
  if (!to.matched.length) {
    // 如果没有匹配到任何路由记录，则重定向到登录页面
    next('/NaN404')
  } else {
    // 如果匹配到了路由记录，则正常跳转，判断跳转是否需要认证
    if (to.meta.isAuth) {
      //先判断用户是否登录
      if (localStorage.getItem('token')) {
        next()
      } else {
        // 未登录跳转登录界面
        next({ name: 'login' })
      }
    } else {
      next()
    }
  }
})

export default router
